package util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.function.Function;

public class ReflectTool {

    private static Function<Field,String> getFieldName=field ->{
        String fieldName=field.getName();
        String firstName=fieldName.substring(0,1);
        String lastName=fieldName.substring(1,fieldName.length());
        return firstName.toUpperCase()+lastName;
    };

    public static String getFieldSetName(Field field){
        return String.format("set%s",getFieldName.apply(field));
    }

    public static String getFieldGetName(Field field){
        return String.format("get%s",getFieldName.apply(field));
    }

    public static Object getFieldValue(Object o,Field field){
        Object resultObj=new Object();
        try {
            Class c=o.getClass();
            String getName=getFieldGetName(field);
            Method method = c.getMethod(getName);
            resultObj = method.invoke(o);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return resultObj;
    }
}
